{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, World <class 'str'>\n",
      "Hello, World <class 'str'>\n",
      "WHellooHellorHellolHellod <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串拼接\n",
    "s = \"Hello, World\"\n",
    "print(s, type(s))\n",
    "\n",
    "# 字符串加法\n",
    "s1 = \"Hello, \"\n",
    "s2 = \"World\"\n",
    "s = s1 + s2\n",
    "print(s, type(s))\n",
    "\n",
    "# 字符串连接，join\n",
    "s1 = \"Hello\"\n",
    "s2 = \"World\"\n",
    "s = s1.join(s2)\n",
    "print(s, type(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd']\n",
      "l\n",
      "l\n"
     ]
    }
   ],
   "source": [
    "s = \"Hello, World\"\n",
    "s1 = list(s)\n",
    "print(s1)\n",
    "print(s1[2])\n",
    "print(s[2])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n",
      "l\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "# 字符串统计\n",
    "#    012345678901\n",
    "s = \"Hello, World\"\n",
    "\n",
    "# 测量字符串长度\n",
    "print(len(s))\n",
    "\n",
    "# 字符串作为序列，它的位置是从0开始的，可以用下标访问字符串的某个字符\n",
    "print(s[3])\n",
    "\n",
    "# 下标 -1 代表最后一个位置的字符\n",
    "print(s[-1])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "klmnopqrst\n",
      "kmoqs\n",
      "klmnopqrstuvwxyz\n",
      "abcdefghij\n",
      "klmnopqrstuvwxy\n",
      "abcdefghijklmnopqrstuvwxyz\n",
      "zyxwvutsrqponmlkjihgfedcba\n"
     ]
    }
   ],
   "source": [
    "# 字符串切片\n",
    "#              1         2\n",
    "#    01234567890123456789012345\n",
    "s = \"abcdefghijklmnopqrstuvwxyz\"\n",
    "print(s[10:20])     # 位置访问，顾头不顾尾\n",
    "print(s[10:20:2])   # 先切片再按步长取\n",
    "print(s[10:])       # 没指定边界时\n",
    "print(s[:10])       # 没指定边界时\n",
    "print(s[10:-1])\n",
    "print(s[:])         # 等价于全选\n",
    "print(s[::-1])      # 等价于反转"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "# 字符串转换为列表\n",
    "s = \"Hello, World\"\n",
    "print(list(s))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Python', 'is', 'an', 'easy', 'to', 'learn', 'and', 'powerful', 'programming', 'language'] <class 'list'>\n",
      "['Python is an easy to learn and powerful', 'programming', 'language'] <class 'list'>\n",
      "['1', 'Tom', 'male', '2000'] <class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串split（分裂）操作（缺省操作）\n",
    "s = \"Python is an easy to learn and powerful programming language\"\n",
    "s1 = s.split()\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 从字符串右侧开始分裂，并且只分裂2次\n",
    "s1 = s.rsplit(' ', 2)\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 字符串指定分裂字符\n",
    "s = \"1,Tom,male,2000\"\n",
    "s1 = s.split(',')   # 指定逗号为分裂字符\n",
    "print(s1, type(s1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  Python is an easy to learn   <class 'str'>\n",
      "Python is an easy to learn <class 'str'>\n",
      "Python is an == easy to learn <class 'str'>\n",
      "Python is an == easy to learn <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "# 字符串压缩\n",
    "s = \"  Python is an easy to learn  \"\n",
    "print(s, type(s))\n",
    "\n",
    "# 压缩字符串两端的空格\n",
    "s1 = s.strip()\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 压缩两端的等号\n",
    "s = \"==Python is an == easy to learn==\"\n",
    "s1 = s.strip('=')\n",
    "print(s1, type(s1))\n",
    "\n",
    "# 压缩不同的字符\n",
    "s = \"¥¥++Python is an == easy to learn--$$\"\n",
    "s1 = s.strip(\"+$¥-\")\n",
    "print(s1, type(s1))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Zen of Python, by Tim Peters\n",
      "\n",
      "Beautiful is better than ugly.\n",
      "Explicit is better than implicit.\n",
      "Simple is better than complex.\n",
      "Complex is better than complicated.\n",
      "Flat is better than nested.\n",
      "Sparse is better than dense.\n",
      "Readability counts.\n",
      "Special cases aren't special enough to break the rules.\n",
      "Although practicality beats purity.\n",
      "Errors should never pass silently.\n",
      "Unless explicitly silenced.\n",
      "In the face of ambiguity, refuse the temptation to guess.\n",
      "There should be one-- and preferably only one --obvious way to do it.\n",
      "Although that way may not be obvious at first unless you're Dutch.\n",
      "Now is better than never.\n",
      "Although never is often better than *right* now.\n",
      "If the implementation is hard to explain, it's a bad idea.\n",
      "If the implementation is easy to explain, it may be a good idea.\n",
      "Namespaces are one honking great idea -- let's do more of those!\n"
     ]
    }
   ],
   "source": [
    "import this"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.', 'Flat is better than nested.']\n"
     ]
    }
   ],
   "source": [
    "# 行分裂\n",
    "s = '''Beautiful is better than ugly.\n",
    "Explicit is better than implicit.\n",
    "Simple is better than complex.\n",
    "Complex is better than complicated.\n",
    "Flat is better than nested.\n",
    "'''\n",
    "\n",
    "s1 = s.splitlines()\n",
    "print(s1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "31\n",
      "31\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "# 字符串查找 find\n",
    "s = \"Python is an easy to learn and powerful programming language\"\n",
    "\n",
    "# 返回查找内容在字符串中的位置\n",
    "pst = s.find('powerful')\n",
    "print(pst)\n",
    "\n",
    "# 指定起始位置查找\n",
    "pst = s.find('powerful', 10)\n",
    "print(pst)\n",
    "\n",
    "# 当没有找到时返回-1\n",
    "pst = s.find('Java')\n",
    "print(pst)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n",
      "python is an easy to learn\n",
      "PYTHON IS AN EASY TO LEARN\n",
      "10 + 12 = 22\n"
     ]
    }
   ],
   "source": [
    "# 字符串其他操作\n",
    "s = \"89c\"\n",
    "print(s.endswith('c'))  # 判断字符串是否以指定字符结束\n",
    "\n",
    "s = \"¥100\"\n",
    "print(s.startswith('¥'))  # 判断字符串是否以指定字符开始\n",
    "\n",
    "s = \"Python is an easy to learn\"\n",
    "print(s.lower())    # 把字符串转换为小写\n",
    "print(s.upper())    # 把字符串转换为大写\n",
    "\n",
    "a = 10\n",
    "b = 12\n",
    "print(f\"{a} + {b} = {a + b}\")   # 字符串的格式化输出"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}